package com.agent.web;

import com.agent.entity.RepaireInfo;
import com.agent.entity.RepairePic;
import com.agent.service.IRepaireInfoService;
import com.agent.service.RepairePicService;
import com.agent.shiro.ShiroUser;
import com.agent.util.MyPropertiesConfigurer;
import com.agent.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@Slf4j
public class RepairePicController {
	@Autowired
	private RepairePicService RepairePicService;
	@Resource(name = "repaireInfoService")
	IRepaireInfoService service;
	private  static final String  REPAIRE_PIC = "repaire";
	
	@RequestMapping("/repairePic/list")
	@ResponseBody
	public Map<String,Object> searchRepairePic(@RequestParam("id") String repaireId) {
		Map<String,Object> result = ResponseData.getSuccessResult();
		List<String> data = new ArrayList<>();
		RepaireInfo repaireInfo = service.getRepaireInfo(repaireId);
		if(repaireInfo !=null && !StringUtils.isEmpty(repaireInfo.getPicAddress())){
			data.add(repaireInfo.getPicAddress());
		}
		RepairePic repairePic = new RepairePic();
		repairePic.setRepaireId(Long.parseLong(repaireId));
		List<String> collect =
			RepairePicService.searchRepairePic(repairePic).stream().map(repairePic1 -> repairePic1.getPicUrl())
				.collect(Collectors.toList());
		data.addAll(collect);
		result.put(ResponseData.DATA,data);
		return result;
	}
	

	
	@RequestMapping(value = "/repairePic/insert",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insert(@RequestParam("files") MultipartFile[] files,@RequestParam("id") String repaireId)
		throws IOException {
		ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		int i = 0;
		for (MultipartFile file : files) {
			if (!file.isEmpty()) {
				String storePath = MyPropertiesConfigurer.ctxPropertiesMap.get("file.storage.path")+"/" +REPAIRE_PIC;
				//                String storePath = request.getRealPath("/") + "images";
				String fileName = file.getOriginalFilename();
				File filepath = new File(storePath, fileName);
				if (!filepath.getParentFile().exists()) {
					filepath.getParentFile().mkdirs();//如果目录不存在，创建目录
				}
				file.transferTo(new File(storePath + "/" + fileName));//把文件写入目标文件地址
				insertData(shiroUser, i, storePath, fileName,Long.parseLong(repaireId));
			}
		}
		return ResponseData.getSuccessResult();
	}

	private void insertData(ShiroUser shiroUser, int i, String storePath, String fileName,Long repaireId) {
		RepairePic repairePic = new RepairePic();
		repairePic.setPicName(fileName);
		repairePic.setPicUrl(storePath +"/"+ fileName);
		repairePic.setCreateTime(new Date());
		repairePic.setCreator(shiroUser.getUserName());
		repairePic.setOrder(i++);
		repairePic.setRepaireId(repaireId);
		RepairePicService.insert(repairePic);
	}

}
